﻿using HECore.Account.Repository.Context;
using HECore.ServiceFrame.Extensions;
using HECore.ServiceFrame.Filters;
using Microsoft.EntityFrameworkCore;

namespace HECore.Account.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // 注册接口、异常过滤处理
            services.AddControllers(x => x.Filters.Add(typeof(ExceptionFilter)));

            // 注冊健康状态检查
            services.AddHealthChecks();

            // 注册数据DbContext
            services.AddDbContext<AccountContext>(p => p.UseSqlServer(Configuration["ServiceConfig:ConnectionStrings:HECore_Account"]));

            // 注册服务层
            services.FrameRegisterService();

            // 注册Swagger
            services.FrameRegisterSwaggerGen();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            // 使用允许跨域
            app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

            // 使用启用健康状态检查中间件
            app.UseHealthChecks("/health");

            // 使用Consul
            app.FrameUseConsul();

            // 使用Swagger UI
            app.FrameUseSwaggerUI();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
